package com.project.xy;

import java.util.ArrayList;
import java.util.List;

public class GradeManager {
    private List<StudentGrade> grades = new ArrayList<>();

    // 添加成绩记录
    public void addGrade(StudentGrade grade) {
        grades.add(grade);
        System.out.println("成绩已成功记录！");
    }

    // 按姓名查询成绩
    public void queryByName(String name) {
        boolean found = false;
        for (StudentGrade grade : grades) {
            if (grade.getName().equals(name)) {
                System.out.println(grade);
                found = true;
            }
        }
        if (!found) {
            System.out.println("未找到符合条件的成绩记录！");
        }
    }

    // 按学号查询成绩
    public void queryById(String id) {
        boolean found = false;
        for (StudentGrade grade : grades) {
            if (grade.getId().equals(id)) {
                System.out.println(grade);
                found = true;
            }
        }
        if (!found) {
            System.out.println("未找到符合条件的成绩记录！");
        }
    }

    // 按课程名称查询成绩
    public void queryByCourse(String course) {
        boolean found = false;
        for (StudentGrade grade : grades) {
            if (grade.getCourse().equals(course)) {
                System.out.println(grade);
                found = true;
            }
        }
        if (!found) {
            System.out.println("未找到符合条件的成绩记录！");
        }
    }
    // 统计课程成绩
    public void statisticsByCourse(String course) {
        double sum = 0;
        int count = 0;
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        boolean found = false;

        for (StudentGrade grade : grades) {
            if (grade.getCourse().equals(course)) {
                double gradeValue = grade.getGrade();
                sum += gradeValue;
                count++;
                if (gradeValue > max) {
                    max = gradeValue;
                }
                if (gradeValue < min) {
                    min = gradeValue;
                }
                found = true;
            }
        }
        if (found) {
            double average = sum / count;
            System.out.printf("课程：%s\n平均分：%.2f\n最高分：%.1f\n最低分：%.1f\n", course, average, max, min);
        } else {
            System.out.println("未找到符合条件的成绩记录！");
        }
    }
}